home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xsok-1.000 / xsok-1 / xsok-1.01 / src / username.c < prev    next >
C/C++ Source or Header  |  1995-10-14  |  2KB  |  84 lines

  1. /*****************************************************************************/
  2. /*                                         */
  3. /*                                         */
  4. /*    Xsok version 1.01 -- module username.c                     */
  5. /*                                         */
  6. /*    Tries to compute a nice user- and hostname (e-mail address).         */
  7. /*    Written by Michael Bischoff (mbi@mo.math.nat.tu-bs.de)             */
  8. /*    November-1994                                 */
  9. /*    changed October 1995                             */
  10. /*    see COPYRIGHT.xsok for Copyright details                 */
  11. /*                                         */
  12. /*                                         */
  13. /*****************************************************************************/
  14. #ifndef _POSIX_SOURCE
  15. #define _POSIX_SOURCE
  16. #endif
  17. #include <unistd.h>
  18. #include <pwd.h>
  19. #include <sys/types.h>
  20. #include <sys/utsname.h>
  21. #include "xsok.h"
  22.  
  23. #ifdef BSD_NETKIT
  24. #include <netdb.h>
  25. #endif
  26.  
  27. char username[256];
  28.  
  29. void buildusername(const char *name) {
  30.     if (name) {
  31.     if (strlen(name) > 255) {
  32.         strncpy(username, name, 255);
  33.         username[256] = '\0';
  34.     } else
  35.         strcpy(username, name);
  36.     } else {
  37.     struct passwd *pp;
  38.     const char *realname, *loginname;
  39.     char fqdn[256];
  40.     struct utsname buf;
  41. #ifdef BSD_NETKIT
  42.     struct hostent *hp;
  43. #endif
  44.     /* getlogin() fails when xsok is called from the fvwm window
  45.        manager menu (why?) and cuserid() has disappeared in POSIX
  46.        1990. We use getuid() now. */
  47.     if ((pp = getpwuid(getuid()))) {
  48.         if (pp->pw_gecos && strchr(pp->pw_gecos, ','))
  49.         *strchr(pp->pw_gecos, ',') = '\0';
  50.         realname = pp->pw_gecos;
  51.         loginname = pp->pw_name;
  52.     } else {
  53.         /* unable to obtain passwd entry */
  54.         realname = NULL;
  55.         loginname = "unknown";
  56.     }
  57.     if (uname(&buf))
  58.         strcpy(buf.nodename, "unknown");
  59. #ifdef BSD_NETKIT
  60.     if ((hp = gethostbyname(buf.nodename)))
  61.         strcpy(fqdn, hp->h_name);
  62.     else
  63. #endif
  64.         sprintf(fqdn, "%s.(unknown)", buf.nodename);
  65.  
  66.     if (realname)
  67.         sprintf(username, "%s (%s@%s)", realname, loginname, fqdn);
  68.     else
  69.         sprintf(username, "%s@%s", loginname, fqdn);
  70.     }
  71. }
  72.  
  73. #ifdef TESTING
  74. int main(int argc, char *argv[]) {
  75.     if (argc == 3 && !strcmp(argv[1], "-u"))
  76.     buildusername(argv[2]);
  77.     else
  78.     buildusername(NULL);
  79.     printf( /* "Automatically generated username is\n" */
  80.        "\"%s\"\n", username);
  81.     return 0;
  82. }
  83. #endif
  84.